package org.infodavid.common.model.impl;

import org.infodavid.common.model.IApplicationSettings;
import org.infodavid.common.model.IGenericPropertiesMap;
import org.infodavid.common.model.IOrganization;
import org.infodavid.common.model.ModelFactory;
import org.infodavid.common.model.impl.AbstractImprovedDataObjectImpl;

/**
 * The Class CityImpl.
 */
public class ApplicationSettingsImpl extends AbstractImprovedDataObjectImpl<Long> implements
    IApplicationSettings {

	/** serialVersionUID. */
	private static final long serialVersionUID = -7949194083812234129L;

	/** The name. */
	private String product;

	/** The properties. */
	private IGenericPropertiesMap properties;

	/** The organization. */
	private IOrganization organization;

	/**
	 * The Constructor.
	 * @throws Exception the exception
	 */
	protected ApplicationSettingsImpl() throws Exception {
		super();
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.IApplicationSettings#getProduct()
	 */
	public String getProduct() {
		return product;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.IApplicationSettings#setProduct(java.lang.String)
	 */
	public void setProduct(final String value) {
		if (!equals(product, value)) {
			final Object old = product;
			product = value;
			markModified(PRODUCT, old, product);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.data.IGenericObject#getProperties()
	 */
	public IGenericPropertiesMap getProperties() {
		return properties;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.impl.DataObjectImpl#init()
	 */
	@Override
	public void init() throws Exception {
		super.init();

		product = null;
		properties = ModelFactory.getInstance().create(IGenericPropertiesMap.class);
		organization = null;
	}

	/**
	 * Sets the properties.
	 * @param values the properties
	 */
	protected void setProperties(final IGenericPropertiesMap values) {
		final Object old = properties;
		properties = values;
		markModified(PROPERTIES, old, properties);
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.IApplicationSettings#getOrganization()
	 */
	public IOrganization getOrganization() {
		return organization;
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.infodavid.common.model.IApplicationSettings#setOrganization(org.infodavid.common.model.
	 * IOrganization)
	 */
	public void setOrganization(final IOrganization value) {
		if (!equals(organization, value)) {
			final Object old = organization;
			organization = value;
			markModified(ORGANIZATION, old, organization);
		}
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.impl.model.DataObjectImpl#toString(java.lang.StringBuffer)
	 */
	@Override
	protected StringBuffer toString(final StringBuffer builder) {
		super.toString(builder);

		builder.append("[product=");

		if (product == null) {
			builder.append("null");
		}
		else {
			builder.append(product);
		}

		builder.append(", properties=");
		builder.append(properties);
		builder.append(", organization=");

		if (organization == null) {
			builder.append("null");
		}
		else {
			builder.append(organization.getKey());
		}

		builder.append("]");

		return builder;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.impl.DataObjectImpl#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		final int result = 1;

		return prime * result + (getProduct() == null ? 0 : getProduct().hashCode());
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.impl.DataObjectImpl#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}

		if (obj == null) {
			return false;
		}

		if (!(obj instanceof IApplicationSettings)) {
			return false;
		}

		final IApplicationSettings other = (IApplicationSettings)obj;

		if (getProduct() == null) {
			if (other.getProduct() != null) {
				return false;
			}
		}
		else if (!getProduct().equals(other.getProduct())) {
			return false;
		}

		return true;
	}
}
